import 'package:flutter/material.dart';
import '../config/JvtdConfig.dart';
import 'package:flutter_statusbar_manager/flutter_statusbar_manager.dart';
import 'package:flutter_redux/flutter_redux.dart';
import '../rudex/JvtdState.dart';
import 'package:redux/redux.dart';
import '../utils/JvtdToastUtils.dart';
import '../string/JvtdString.dart';
import 'package:url_launcher/url_launcher.dart';

abstract class BasePageState<T extends StatefulWidget> extends State<T> {
  bool isStatusBar = true; //是否进行状态栏修改
  StatusBarStyle statusBarStyle = StatusBarStyle.LIGHT_CONTENT; //状态栏字体颜色
  Color statusBarColor = Colors.transparent; //状态栏颜色
  bool statusBarTranslucent = false; //是否浸入式
  bool isAppBar = true; //是否显示appbar
  Color bgColor = Colors.white; //背景颜色
  AppBar appBar; //自定义appbar

  bool isDoubleClick = false; //是否双击返回桌面
  DateTime _clickTime; //返回键点击时间

  Store mStore;

  @protected
  Widget buildBody(BuildContext context);

  Widget buildNavigationBar(BuildContext context) {
    return null;
  }

  //appbar 标题
  String appBarTitle(BuildContext context) {
    return null;
  }

  Widget _buildBase(BuildContext context) {
    return Scaffold(
      appBar: isAppBar
          ? (appBar == null
              ? AppBar(
                  title: Text(appBarTitle(context)),
                  centerTitle: true,
                )
              : appBar)
          : null,
      body: buildBody(context),
      bottomNavigationBar: buildNavigationBar(context),
      backgroundColor: bgColor,
    );
  }

  @override
  Widget build(BuildContext context) {
    FlutterStatusbarManager.setStyle(statusBarStyle);
    FlutterStatusbarManager.setColor(statusBarColor);
    FlutterStatusbarManager.setTranslucent(statusBarTranslucent);
    return StoreBuilder<JvtdState>(builder: (context, store) {
      mStore = store;
      return Localizations.override(
        context: context,
        locale: store.state.locale,
        child: isDoubleClick
            ? WillPopScope(
                child: _buildBase(context),
                onWillPop: _onWillPop,
              )
            : _buildBase(context),
      );
    });
  }

  Future<bool> _onWillPop() {
    if (_clickTime != null &&
        DateTime.now().difference(_clickTime) < Duration(seconds: 2)) {
      return Future.value(true);
    } else {
      _clickTime = DateTime.now();
      JvtdToastUtils.showMessage(msg: JvtdString.getLocale(context).exit_app);
      return Future.value(false);
    }
  }
}
